package com.dnd.action.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;



import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Abstract Action to support login detection
 * 
 * @author luoj
 * 
 */
public abstract class AbstractAction extends ActionSupport {

	protected HttpServletRequest request = null;
	protected HttpServletResponse response = null;
	protected HttpSession session = null;

	protected String userID = null;

	public String getUserID() {
		return userID;
	}

	public void setUserID(String userID) {
		this.userID = userID;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	protected String userName = null;

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public HttpSession getSession() {
		return session;
	}

	@Override
	public String execute() throws Exception {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		session = request.getSession();


		if (!loginAlready())
			return Action.LOGIN;
		return doAction();
	}

	private boolean loginAlready() {
		if (true) {
			return true;
		} else {
			this.userID = (String) session
					.getAttribute(SessionAttributes.USER_ID);
			this.userName = (String) session
					.getAttribute(SessionAttributes.USER_NAME);
			if (userID == null) {
				return false;
			} else {
				return true;
			}
		}
	}

	protected abstract String doAction() throws Exception;
}
